#include "login.h"

//判断是否能够登录
int jg_login(int netFD){

    //获取登录ID
    printf("请输入用户ID:\n");
    int id;
    scanf("%d", &id);
    printf("id:%d\n", id);
    send(netFD, &id, sizeof(id), MSG_NOSIGNAL);
    while(getchar()!='\n');
    //获取登录密码
    printf("请输入用户密码:\n");
    char buf1[12] = { 0 };
    int ret1 = read(STDIN_FILENO, buf1, sizeof(buf1));
    ERROR_CHECK(ret1, -1, "read1_jgLogin.c");

    buf1[11] = '\n';
    char *p1 = strtok(buf1, "\n");
    send(netFD, p1, strlen(p1), MSG_NOSIGNAL);

    //服务器发送消息回来
    //返回值-1，表示不存在 -2表示密码错误,0表示登陆成功
    int num;
    recv(netFD, &num, sizeof(num), MSG_WAITALL);
    switch(num){
    case -1:
        printf("ID错误，不存在\n");
        break;
    case -2:
        printf("输入的密码不正确\n");
        break;
    case 0:
        printf("登陆成功\n");
        return 0;
    }
    return -1;
}
